package zpin.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.compass.core.support.search.CompassSearchCommand;
import org.compass.core.support.search.CompassSearchResults;

import com.opensymphony.xwork2.ActionSupport;

import util.compass.CompassSearchService;
import util.dao.Pagination;
import zpin.dao.AuthorizationDao;
import zpin.dao.CityDao;
import zpin.dao.CompanyDao;
import zpin.dao.EngageDao;
import zpin.dao.TypeDao;
import zpin.model.Authorization;
import zpin.model.City;
import zpin.model.Company;
import zpin.model.Engage;
import zpin.model.Resume;
import zpin.model.Type;

public class EngageAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Long id;

	private String target;

	private Authorization authorization;

	private Engage engage;

	private City city;

	private Type type;

	private Company company;

	private Resume resume;

	private AuthorizationDao authorizationDao;

	private CityDao cityDao;

	private TypeDao typeDao;

	private CompanyDao companyDao;

	private EngageDao engageDao;

	List<Engage> engagelist = new ArrayList<Engage>();

	List<City> citylist = new ArrayList<City>();

	List<Type> typelist = new ArrayList<Type>();

	List<Resume> resumelist = new ArrayList<Resume>();

	Pagination<Engage> pagination;

	private CompassSearchResults results;

	private CompassSearchService compassSearchService;

	public Resume getResume() {
		return resume;
	}

	public void setResume(Resume resume) {
		this.resume = resume;
	}

	public List<Resume> getResumelist() {
		return resumelist;
	}

	public void setResumelist(List<Resume> resumelist) {
		this.resumelist = resumelist;
	}

	public Authorization getAuthorization() {
		return authorization;
	}

	public void setAuthorization(Authorization authorization) {
		this.authorization = authorization;
	}

	public AuthorizationDao getAuthorizationDao() {
		return authorizationDao;
	}

	public void setAuthorizationDao(AuthorizationDao authorizationDao) {
		this.authorizationDao = authorizationDao;
	}

	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}

	public Type getType() {
		return type;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public CityDao getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}

	public TypeDao getTypeDao() {
		return typeDao;
	}

	public void setTypeDao(TypeDao typeDao) {
		this.typeDao = typeDao;
	}

	public CompanyDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	public Engage getEngage() {
		return engage;
	}

	public void setEngage(Engage engage) {
		this.engage = engage;
	}

	public EngageDao getEngageDao() {
		return engageDao;
	}

	public void setEngageDao(EngageDao engageDao) {
		this.engageDao = engageDao;
	}

	public List<Engage> getEngagelist() {
		return engagelist;
	}

	public void setEngagelist(List<Engage> engagelist) {
		this.engagelist = engagelist;
	}

	public List<City> getCitylist() {
		citylist = cityDao.findAll();
		return citylist;
	}

	public void setCitylist(List<City> citylist) {
		this.citylist = citylist;
	}

	public List<Type> getTypelist() {
		typelist = typeDao.findAll();
		return typelist;
	}

	public void setTypelist(List<Type> typelist) {
		this.typelist = typelist;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public CompassSearchResults getResults() {
		return results;
	}

	public void setResults(CompassSearchResults results) {
		this.results = results;
	}

	public CompassSearchService getCompassSearchService() {
		return compassSearchService;
	}

	public void setCompassSearchService(
			CompassSearchService compassSearchService) {
		this.compassSearchService = compassSearchService;
	}

	public Pagination<Engage> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<Engage> pagination) {
		this.pagination = pagination;
	}

	public String create() {
		company = (Company) ServletActionContext.getRequest().getSession()
				.getAttribute("authorization");
		engage.setCompany(company);
		engageDao.create(engage);
		return SUCCESS;
	}

	public String delete() {
		engage = engageDao.findById(id);
		engageDao.delete(engage);
		return SUCCESS;
	}

	public String update() {
		engage = engageDao.findById(id);
		if (engage.getId() == null) {
			engageDao.create(engage);
		} else {
			engageDao.delete(engage);
		}
		return SUCCESS;
	}

	public String selectByCompany() {
		company = (Company) ServletActionContext.getRequest().getSession()
				.getAttribute("authorization");
		engagelist = engageDao.findByCompany(company.getId());
		return SUCCESS;
	}

	public String select() {
		if (pagination == null) {
			this.pagination = new Pagination<Engage>();
		}
		if (pagination.getSize() == 0) {
			this.pagination.setSize(2);
		}
		if (pagination.getNo() == 0) {
			this.pagination.setNo(1);
		}
		engageDao.browse(pagination);
		engagelist = pagination.getResults();
		return SUCCESS;
	}

	public String execute() {
		citylist = cityDao.findAll();
		typelist = typeDao.findAll();
		return SUCCESS;
	}

	public String search() {
		results = compassSearchService.search(new CompassSearchCommand(target,
				0));
		for (int i = 0; i < results.getHits().length; i++) {
			engagelist.add((Engage) results.getHits()[i].getData());
		}
		return SUCCESS;
	}

	public String selectpastengage() {
		engage = engageDao.findById(id);
//		while (engage.getResumes().iterator().hasNext()) {
//			Resume resume = new Resume();
//			resume = engage.getResumes().iterator().next();
//			resumelist.add(resume);
//		}
		for(int i=0;i<engage.getResumes().size();i++){
			resume = engage.getResumes().iterator().next();
			resumelist.add(resume);
		}
		return SUCCESS;
	}
}
